/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.jg.report.freight.information.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.third.jg.report.freight.information.constant.NmgReportConstant;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.ReportDocTypeEnum;
import org.jeecg.modules.third.jg.report.freight.information.dto.AppInfoDTO;
import org.jeecg.modules.third.jg.report.freight.information.model.R;
import org.jeecg.modules.third.jg.report.freight.information.model.Root;
import org.jeecg.modules.third.jg.report.freight.information.util.sm.SM2Util;
import org.jeecg.modules.third.jg.report.freight.information.util.sm.SM4Util;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 内蒙古平台上报服务，交科院系统
 */
@Service
@Slf4j
public class NmgReportPlatformService {

    @Resource
    private  RedisUtil redisUtil;

    private String reportPlatformDomain;

    private String username;

    private String password;

    private String debugSystem;

    private String appId;

    private String appSecurity;

    private String appSenderCode;

    private String appDebugSystem;

    public AppInfoDTO getAppInfo() {
        return AppInfoDTO.builder()
                .appId(this.appId)
                .appSecurity(this.appSecurity)
                .enterpriseSenderCode(this.appSenderCode)
                .debugSystem(this.appDebugSystem)
                .build();
    }

    public void setReportInfo(String reportPlatformDomain,
                              String username,
                              String password,
                              String publickey,
                              String appId,
                              String appSecurity,
                              String appSenderCode,
                              String appDebugSystem) {
        //联调状态的App需要设置debugSystem
        if ("0".equals(appDebugSystem)) {
            appDebugSystem = "debugSystem";
        }

        this.reportPlatformDomain = reportPlatformDomain;
        this.username = username;
        this.password = password;
        this.appId = appId;
        this.appSecurity = appSecurity;
        this.appSenderCode = appSenderCode;
        this.appDebugSystem = appDebugSystem;
    }

    /**
     * 获取到的公钥
     * @return
     */
    private String getRSAPublic() throws Exception {
        return StringUtils.EMPTY;
    }


    private String getLoginToken () throws Exception{
        return StrUtil.EMPTY;
    }

    /**
     * 组装交换报文
     * @param reportDocTypeEnum 上报单据类型
     * @param jsonString 报文内容
     * @return
     */
    private Root messageAssembly(ReportDocTypeEnum reportDocTypeEnum ,String jsonString)
            throws Exception {
        return null;
    }

    /**
     * 发送上报
     * @param reportDocTypeEnum
     * @param jsonString
     */
    public String sendReport(ReportDocTypeEnum reportDocTypeEnum ,String jsonString)  throws Exception {
        String sendResult = StringUtils.EMPTY;
        try {
            Root exchangeModel = messageAssembly(reportDocTypeEnum, jsonString);
            if (exchangeModel != null) {
                //发送报文
                String url = StringUtils.isEmpty(this.debugSystem) ?
                        reportPlatformDomain + NmgReportConstant.PROD_SEND_URL :
                        reportPlatformDomain + NmgReportConstant.TEST_SEND_URL;
                log.info("内蒙古监管-> {}", JSONUtil.toJsonStr(exchangeModel));
                log.info("内蒙古监管上报请求 -> {} -> {} -> {}", url, exchangeModel.getMessageReferenceNumber() , jsonString);
                sendResult = HttpUtil.post(url, JSON.toJSONString(exchangeModel));
                log.info("内蒙古监管上报回执 <- {}", sendResult);
                return sendResult;
            }
        } catch(Exception e) {
            log.info("内蒙古上报交换异常{}",  e.getMessage());
            throw e;
        }

        return sendResult ;
    }

}
